// stores/permission.js
import { defineStore } from 'pinia'
import { authRoutes } from '@/router/routes'

export const usePermissionStore = defineStore('permission', {
  state: () => ({
    routes: [],
    hasGenerated: false // 新增生成状态标识
  }),

  actions: {
    // 修改后的路由生成方法
    async generateRoutes(roles) {
      try {
        if (this.hasGenerated) return this.routes
        
        // 深度过滤路由
        const filterRoutes = (routes) => {
          return routes.filter(route => {
            const hasPermission = !route.meta?.roles || route.meta.roles.includes(roles)
            const validChildren = route.children?.length ? filterRoutes(route.children) : []
            return hasPermission && !route.meta?.hidden && (validChildren.length || route.component)
          })
        }
        
        this.routes = filterRoutes(authRoutes)
        this.hasGenerated = true
        return this.routes
      } catch (error) {
        console.error('路由生成失败:', error)
        return []
      }
    }
  }
})

function filterAsyncRoutes(routes, roles) {
  // 自定义过滤逻辑
}
